package integration.data.task.api.transformation.impl;

import cn.hutool.core.util.StrUtil;
import integration.data.task.api.transformation.Transformer;
import integration.modle.exception.IntegrationValidException;
import io.github.bigbird0101.datatransfer.model.element.Column;
import io.github.bigbird0101.datatransfer.model.element.StringColumn;
import io.github.bigbird0101.datatransfer.record.Record;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Map;

import static integration.data.task.api.constant.Constant.*;

/**
 * 数据替换
 */
@Slf4j
public class DataReplace extends Transformer {

    public DataReplace() {
        setTransformerName(DATA_REPLACE);
    }

    @Override
    public Record evaluate(Record record, Map<String, Object> paras) {

        int columnIndex;
        String oldValue;
        String newValue;
        String match;
        Integer caseSensitive;
        boolean isAllMatching;
        boolean noticeCase;
//        LogUtils.info(log,"paras: {}", paras);

        try {
            if (paras.size() != 5) {
                throw new IntegrationValidException("替换参数必须是5个");
            }

            columnIndex = (Integer) paras.get(COLUMN);
            oldValue = (String) paras.get(OLD_VALUE);
            newValue = (String) paras.get(NEW_VALUE);
            match = (String) paras.get(MATCH);
            caseSensitive = (Integer) paras.get(CASE_SENSITIVE);

            isAllMatching = !MATCH_FUZZ.equalsIgnoreCase(match);
            noticeCase = caseSensitive == 1;

        } catch (Exception e) {
            throw new IntegrationValidException("数据替换参数异常" + "paras:" + Arrays.asList(paras).toString() + " => " + e.getMessage());
        }

        Column column = record.getColumn(columnIndex);

        try {
            String oriValue = column.asString();

            //如果字段为空，跳过replace处理
            if (oriValue == null) {
                return record;
            }


            /*
             全量-忽略大小写:
                oriValue = "this is admin", oldValue = "admin" false
                oriValue = "admin", oldValue = "Admin" true
             全量-不忽略大小写:
                oriValue = "admin", oldValue = "admin" true
                oriValue = "admin", oldValue = "Admin" false

             模糊-忽略大小写:
                oriValue = "this is admin", oldValue = "admin" true
                oriValue = "admin", oldValue = "Admin" true
             模糊-不忽略大小写:
                oriValue = "this is admin", oldValue = "admin" true
                oriValue = "admin", oldValue = "Admin" false
             */

            String newStr;

            // 是否全量匹配
            if (isAllMatching) {
                newStr = allMatchingReplace(oriValue, oldValue, newValue, noticeCase);
            } else {
                // 是否忽略大小写
                if (!noticeCase) {
                    newStr = StrUtil.replaceIgnoreCase(oriValue, oldValue, newValue);
                } else {
                    newStr = oriValue.replace(oldValue, newValue);
                }
            }

            record.setColumn(columnIndex, new StringColumn(newStr));

        } catch (Exception e) {
            throw new IntegrationValidException("过滤运行异常", e);
        }
        return record;
    }

    /**
     * 全量匹配替换
     *
     * @param oriValue
     * @param oldValue
     * @param newValue
     * @param noticeCase
     * @return
     */
    private String allMatchingReplace(String oriValue, String oldValue, String newValue, boolean noticeCase) {
        String newStr;
        boolean flag;
        if (!noticeCase) {
            flag = oriValue.toLowerCase().matches(oldValue.toLowerCase());
        } else {
            flag = oriValue.matches(oldValue);
        }

        if (flag) {
            newStr = newValue;
        } else {
            newStr = oriValue;
        }
        return newStr;
    }

}
